﻿using CleanToolkit.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace CleanToolkit.Comparison.DiffModel
{
    [XmlRootAttribute("SchemaFieldsDiff", Namespace="http://www.mondeil.fr")]
    public class SchemaFieldsDiff
    {
        /// <summary>
        /// Liste des SPFields en différence
        /// </summary>
        public List<FieldDiffs> FieldDiffs { get; set; }

        /// <summary>
        /// Constructeur par defaut
        /// </summary>
        public SchemaFieldsDiff()
        {
            FieldDiffs = new List<FieldDiffs>();
        }

        /// <summary>
        /// Constructeur avec Liste des différences
        /// </summary>
        public SchemaFieldsDiff(List<FieldDiffs> diffs)
        {
            this.FieldDiffs = diffs;
        }

        /// <summary>
        /// Serialization
        /// <see cref="http://msdn.microsoft.com/fr-fr/library/c9ww5xa7(v=vs.80).aspx"/>
        /// </summary>
        public XDocument Serialize()
        {
            Type[] overRideTypes = new Type[] { typeof(FieldDiffInfo) };

            XDocument result = XSerializer.Serialize<SchemaFieldsDiff>(this, overRideTypes);

            return result;
        }

        /// <summary>
        /// Enregistrement de la dérialisation xml dans un fichier
        /// </summary>
        /// <param name="s_filePath"></param>
        /// <returns></returns>
        public bool SaveXmlToFile(string s_filePath)
        {
            bool result = true;

            try
            {
                XDocument xSaveDoc = this.Serialize();
                xSaveDoc.Save(s_filePath);
            }
            catch (Exception excSave)
            {
                // $TODO : Logger
            }

            return result;
        }
    }
}
